﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;

namespace Hartley.BusinessLogic
{
  public  class FundManager
    {
      HCPPADataContext hcDc;

      public FundManager(HCPPADataContext hcDc)
     {
            this.hcDc = hcDc;
     }


      public string SaveFund(FundDTO fundDTO)
      {
          string result = "";
          var fnd = hcDc.hcFunds.Where(n => n.FundId == fundDTO.FundId).SingleOrDefault();
          if (fnd == null)
          {
              var fundName = hcDc.hcFunds.Where(a => a.Title == fundDTO.Title).SingleOrDefault();
              if (fundName == null)
              {
                  try
                  {

                      hcFund fund = new hcFund
                      {
                          FundId = fundDTO.FundId,
                          Title = fundDTO.Title,
                          Description = fundDTO.Description,
                          DonatedBy = fundDTO.DonatedBy,
                          Purpose = fundDTO.Purpose,
                          Value = fundDTO.Value,
                          ModifiedBy = fundDTO.ModifiedBy,
                          ModifiedTime = fundDTO.ModifiedTime
                      };
                      hcDc.hcFunds.InsertOnSubmit(fund);
                      result = "Saved";
                  }

                  catch
                  {
                      throw;
                  }
              }
                  
              else
              {
                  //Dont add the same fund again
                  result = "AlreadyExists";

              }

          }
          else
          {
              //edit
              result = "Updated";
          }

          hcDc.SubmitChanges();
          return result;
      }



      public string SaveScholarShip(ScholarshipDTO scholarshipDTO)
      {
          string result = "";
          var sclrshp = hcDc.hcScholarships.Where(n => n.ScholarshipId == scholarshipDTO.ScholarshipId).SingleOrDefault();
          if (sclrshp == null)
          {
              var scholarshipName = hcDc.hcScholarships.Where(a => a.Title == scholarshipDTO.Title).SingleOrDefault();
              if (scholarshipName == null)
              {
                  try
                  {

                      hcScholarship scholarship = new hcScholarship
                      {
                          ScholarshipId = scholarshipDTO.ScholarshipId,
                          Title = scholarshipDTO.Title,
                          Description = scholarshipDTO.Description,
                          Condition = scholarshipDTO.Condition,
                          InMemorialOf = scholarshipDTO.InMemorialOf,
                          Value = scholarshipDTO.Value,
                          OfferedBy = scholarshipDTO.OfferedBy,
                          Capital = scholarshipDTO.Capital,
                          ModifiedBy = scholarshipDTO.ModifiedBy,
                          ModifiedTime = scholarshipDTO.ModifiedTime
                      };
                      hcDc.hcScholarships.InsertOnSubmit(scholarship);
                      result = "Saved";
                  }

                  catch
                  {
                      throw;
                  }
              }

              else
              {
                  //Dont add the same fund again
                  result = "AlreadyExists";

              }

          }
          else
          {
              //edit
              result = "Updated";
          }

          hcDc.SubmitChanges();
          return result;
      }


      public string SaveScholarShipData(ScholarshipDataDTO scholarshipDataDTO)
      {
          string result = "";
          var sclrshpDta = hcDc.hcScholarshipDatas.Where(n => n.ScholarshipDataId == scholarshipDataDTO.ScholarshipDataId).SingleOrDefault();
          if (sclrshpDta == null)
          {
          try
             {

                hcScholarshipData scholarshipData = new hcScholarshipData
                      {
                          ScholarshipDataId = scholarshipDataDTO.ScholarshipDataId,
                          ScholarshipId = scholarshipDataDTO.ScholarshipId,
                          StudentId = scholarshipDataDTO.StudentId,
                          Amount = scholarshipDataDTO.amount,
                          AmountSettledAt = scholarshipDataDTO.amountSettledAt,
                          AcknowledgedAt = scholarshipDataDTO.AcknowledgeAt,
                          SettledBy = scholarshipDataDTO.SettledBy,
                          Notes = scholarshipDataDTO.Notes,
                          ModifiedBy = scholarshipDataDTO.ModifiedBy,
                          ModifedAt = scholarshipDataDTO.ModifiedTime
                      };
                      hcDc.hcScholarshipDatas.InsertOnSubmit(scholarshipData);
                      result = "Saved";
                  }

                  catch
                  {
                      throw;
                  }
              

             

          } else
          {
              //edit
              result = "Updated";
          }

          hcDc.SubmitChanges();
          return result;
      }

      public List<FundDTO> GetFundById(Guid FundId)
      {
          return hcDc.hcFunds.Where(a => a.FundId == FundId).Select(s => new FundDTO
          {
              FundId = s.FundId,
              Title = s.Title,
              Description = s.Description,
              Purpose = s.Purpose,
              DonatedBy = s.DonatedBy
          }).ToList();

      }

      public List<ScholarshipDTO> GetAllScholarShip()
      {
          return hcDc.hcScholarships.Select(s => new ScholarshipDTO
          {
              ScholarshipId = s.ScholarshipId,
              Title = s.Title,
              Description = s.Description,
              Value = s.Value,
              Condition = s.Condition,
              InMemorialOf = s .InMemorialOf,
              NavigationUrl = "viewScholarshipById.aspx?"+s.ScholarshipId,
              OfferedBy = s.OfferedBy
              
          }).ToList();

      }

      public List<ScholarshipDTO> GetScholarShipById(Guid scholarshipId)
      {
          return hcDc.hcScholarships.Where(a=>a.ScholarshipId == scholarshipId).Select(s => new ScholarshipDTO
          {
              ScholarshipId = s.ScholarshipId,
              Title = s.Title,
              Description = s.Description,
              Value = s.Value,
              Condition = s.Condition,
              InMemorialOf = s.InMemorialOf,
              NavigationUrl = "viewScholarshipById.aspx?" + s.ScholarshipId,
              OfferedBy = s.OfferedBy

          }).ToList();

      }

      public List<ScholarshipDataDTO> GetAllScholarShipData()
      {
          return hcDc.hcScholarshipDatas.Select(s => new ScholarshipDataDTO
          {
              ScholarshipId = s.ScholarshipId,
              StudentId = s.StudentId,
              ScholarshipDataId = s.ScholarshipDataId,
              Notes = s.Notes,
              amountSettledAt = s.AmountSettledAt,
              amount = s.Amount,
              SettledBy = new Guid(s.SettledBy.Value.ToString()),
              AcknowledgeAt = s.AcknowledgedAt
             

          }).ToList();

      }


      public List<ScholarshipDataDTO> GetAllScholarShipDataByScholarShipId(Guid scholarshipId)
      {
          return hcDc.hcScholarshipDatas.Where(a=>a.ScholarshipId == scholarshipId).Select(s => new ScholarshipDataDTO
          {
              ScholarshipId = s.ScholarshipId,
              StudentId = s.StudentId,
              ScholarshipDataId = s.ScholarshipDataId,
              Notes = s.Notes,
              amountSettledAt = s.AmountSettledAt,
              amount = s.Amount,
              SettledBy = new Guid( s.SettledBy.Value.ToString()),
              AcknowledgeAt = s.AcknowledgedAt


          }).ToList();

      }



      public List<ScholarshipDataDTO> GetAllScholarShipDataOfStudent(Guid studentId)
      {
          return hcDc.hcScholarshipDatas.Where(a=>a.StudentId == studentId).Select(s => new ScholarshipDataDTO
          {
              ScholarshipId = s.ScholarshipId,
              StudentId = s.StudentId,
              ScholarshipDataId = s.ScholarshipDataId,
              Notes = s.Notes,
              amountSettledAt = s.AmountSettledAt,
              amount = s.Amount,
              SettledBy = new Guid(s.SettledBy.Value.ToString()),
              AcknowledgeAt = s.AcknowledgedAt


          }).ToList();

      }




      public List<FundDTO> GetAllFund()
      {


          return hcDc.hcFunds.OrderBy(c => c.Title).Select(s => new FundDTO
          {
              FundId = s.FundId,
              Title = s.Title,
              Description = s.Description,
              Purpose = s.Purpose,
              DonatedBy = s.DonatedBy,
              NavigationUrl = "viewFund.aspx?" + s.FundId
          }).ToList();

      }


      public void deleteFundById(Guid fundId)
      {
          hcFund fund = hcDc.hcFunds.Where(a=> a.FundId == fundId).SingleOrDefault();
          hcDc.hcFunds.DeleteOnSubmit(fund);
      }
      
    }
}
